package cn.itrip.trade.service;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.dao.hotel.ItripHotelMapper;
import cn.itrip.dao.hotelorder.ItripHotelOrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("orderService")
public class OrderServiceImpl implements  OrderService{

    @Resource(name = "itripHotelOrderMapper")
    private ItripHotelOrderMapper itripHotelOrderMapper;


    @Override
    public ItripHotelOrder getHotelOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("orderNo",orderNo);
        try {
            List<ItripHotelOrder> list = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
            if(list.size()>0){
                return list.get(0);
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }
    /**
     *  支付成功后的处理（需要减少库存）
     *  @param  payType 支付类型 微信?支付宝1
     *  @param  tradeNo 支付宝返回的交易编号
     */
    @Override
    public void paySucces(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order = null;
        order = this.getHotelOrderByOrderNo(orderNo);
        order.setPayType(1);
        order.setOrderStatus(2);//2代表支付成功
        order.setTradeNo(tradeNo);
        //更新订单
        this.itripHotelOrderMapper.updateItripHotelOrder(order);

        /*
        更新库存，由业务提供API，在这里进行调用
         */

    }
    /**
     *  支付失败后的处理（不需要减少库存）
     *  支付成功 --orderstatus=2
     *  支付失败 --orderstatu=1
     *  @param  payType 支付类型 微信?支付宝1
     *  @param  tradeNo 支付宝返回的交易编号
     */
    @Override
    public void payFaild(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order = null;
        order = this.getHotelOrderByOrderNo(orderNo);
        order.setPayType(1);
        order.setOrderStatus(1);//1代表支付失败
        order.setTradeNo(tradeNo);
        //更新订单
        this.itripHotelOrderMapper.updateItripHotelOrder(order);
    }


}
